var flag = false; 
var _currentIndex = 0;
//页面初始加载
efform_onload = function() {
	$('#ef_tab_y').hide(); 
}; 
//TAB切换
function changeTab(indexclicked)
{
	var info = new EiInfo();
	info.set("fdProduceplanId",$('#fdProduceplanId').val()); 
	info.set("fdProfessionalplanId",$('#fdProfessionalplanId').val()); 
	info.set("fdPlanYear",$('#_planYear').val()); 
	info.set("fdPlanMonth",$('#_planMonth').val());  
	if(indexclicked ==0)
	{
		EiCommunicator.send("ZMCP15", "initLoad", info, ajax_zc_callback);
	}else if(indexclicked ==1)
	{
		EiCommunicator.send("ZMCP16", "initLoad", info, ajax_zk_callback);
	}else if(indexclicked ==2)
	{
		EiCommunicator.send("ZMCP17", "initLoad", info, ajax_glaz_callback);
	}  
}

function fundiv(currentIndex,index){  
	_currentIndex=index;
	changeTab(index);
}

/**回调*/
//查询钻尺回调
var ajax_zc_callback = {
	onSuccess : function(eiInfo) {
		efwindow.hide();
		var grid = efgrid.getGridObject("ef_grid_zc");
		grid.refresh(eiInfo);
	},
	onFail : function(eMsg) {
		EFAlert("新增失败，原因：" + eMsg);
	}
}
//查询钻孔回调
var ajax_zk_callback = {
	onSuccess : function(eiInfo) {
		efwindow.hide();
		var grid = efgrid.getGridObject("ef_grid_zk");
		grid.refresh(eiInfo);
	},
	onFail : function(eMsg) {
		EFAlert("新增失败，原因：" + eMsg);
	}
}
//查询管路安装回调
var ajax_glaz_callback = {
	onSuccess : function(eiInfo) {
		efwindow.hide();
		var grid = efgrid.getGridObject("ef_grid_glaz");
		grid.refresh(eiInfo);
	},
	onFail : function(eMsg) {
		EFAlert("新增失败，原因：" + eMsg);
	}
}

/****************************************************************************
 * 主表操作 查询
 *****************************************************************************/
//主表查询
button_query_onclick = function () 
{   	
	efgrid.submitInqu( "ef_grid_result", "ZM","ZMCP14","query");
} 
//主表修改
button_update_onclick = function() {
	var grid=isSelected("ef_grid_result");
	if (grid == 0)return; 
	var rows=grid.getCheckedRows();
	for(var i=0;i<rows.length;i++)
	{
		var now = new Date();
		var year = grid.getCellValueByColumnName(rows[i],"fdPlanYear");
		var fdPlanMonth = grid.getCellValueByColumnName(rows[i],"fdPlanMonth");
		grid.setCellValue(rows[i],3,year+"年"+fdPlanMonth+"月的抽采计划",TYPE_DATA);
	} 
	efgrid.submitForm("ef_grid_result", "ZM", "ZMCP14", "update", true);
} 
//主表新增
button_insert_onclick = function() {
	var grid=isSelected("ef_grid_result");
	if (grid == 0)return; 
	var rows=grid.getCheckedRows();
	for(var i=0;i<rows.length;i++)
	{
		var now = new Date();
		var year = grid.getCellValueByColumnName(rows[i],"fdPlanYear");
		var fdPlanMonth = grid.getCellValueByColumnName(rows[i],"fdPlanMonth");
		grid.setCellValue(rows[i],3,year+"年"+fdPlanMonth+"月的抽采计划",TYPE_DATA);
	} 
	efgrid.submitForm("ef_grid_result", "ZM", "ZMCP14", "insert", true);
} 
//主表删除
button_delete_onclick = function() {
	var grid = isSelected("ef_grid_result");
	if (grid == 0) {
		return;
	}
	var rows = grid.getCheckedRows();
	for (var i = 0; i < rows.length; i++) {
		if (grid.getCellValueByColumnName(rows[i], "fdPlanStatus") != "草拟") {
			EFAlert("只能对草拟的记录进行删除！");
			return;
		}
	}
	EFConfirm('你确认要删除吗?', '确认框',
	function(r) {
		if (r)
			efgrid.submitForm("ef_grid_result", "ZM", "ZMCP14","delete", true);
	});
} 
//复制
button_copy_onclick = function () 
{
	var grid=isSelected("ef_grid_result");
	if (grid == 0)return; 
	var rows=grid.getCheckedRows();
	if(rows.length>1){
		EFAlert("只能选择一条信息！","警告"); return;
	}
	EFConfirm('你确认要复制吗?', '确认框', function(r) {
		if(r)efgrid.submitForm( "ef_grid_result", "ZM","ZMCP14","copy",true);
	});

}

//上报按钮事件
button_report_onclick = function() {
	var grid = isSelected("ef_grid_result");
	if (grid == 0) {
		return;
	}
	var rows = grid.getCheckedRows();
	for (var i = 0; i < rows.length; i++) {
		if (grid.getCellValueByColumnName(rows[i], "fdPlanStatus") == "上报") {
			EFAlert("已经上报的记录，不能再次上报！");
			return;
		}
	}
	EFConfirm('你确认要上报吗?', '确认框',
	function(r) {
		if (r)
			efgrid.submitForm("ef_grid_result", "ZM", "ZMCP14", "report", true);
	});
}
//点击主表撤销按钮
button_goback_onclick = function (){  
	var grid=isSelected("ef_grid_result");
	if (grid == 0)return; 
	var rows=grid.getCheckedRows();
	if (rows.length > 1) {
		EFAlert("一次只能撤回一条记录！","警告"); return;
	}
	for(var i=0;i<rows.length;i++)
	{
		if(grid.getCellValueByColumnName(rows[i],"fdPlanStatus")!="上报")
		{
			EFAlert("只能撤回上报状态的抽采计划！","警告"); return;
		}
	}
	EFConfirm('你确认要撤回吗?', '确认框', function(r) {
		if(r)efgrid.submitForm( "ef_grid_result", "ZM", "ZMCP14", "goback", true);
	});
}
/**
 * 点击主表记录 
 * @param grid_id
 * @param row_index
 */
efgrid_onRowClicked = function(grid_id, row_index) { 

	if (grid_id == "ef_grid_result") {
		var grid = efgrid.getGridObject(grid_id);
		var fdProfessionalplanId = grid.getCellValueByColumnName(row_index,	"fdKeyId");
		var fdPlanYear = grid.getCellValueByColumnName(row_index, "fdPlanYear");
		var fdPlanMonth = grid.getCellValueByColumnName(row_index, "fdPlanMonth");
		var fdPlanStatus = grid.getCellValueByColumnName(row_index, "fdPlanStatus");
		// 隐藏/显示从表按钮
		if (fdPlanStatus != '草拟') {
			hideAndShowSubButtons(false);
		} else {
			hideAndShowSubButtons(true);
		} 
		$('#fdProfessionalplanId').val(fdProfessionalplanId);
		$('#fdPlanYear').val(fdPlanYear);
		$('#fdPlanMonth').val(fdPlanMonth);
		$('#fdPlanStatus').val(fdPlanStatus); 
		changeTab(_currentIndex);
		$('#ef_tab_y').show();
	}
}



/************************************************************************************
 * 钻尺从表操作
 *************************************************************************************/
button_insertdetailzc_onclick = function() {
	if (isSelected("ef_grid_zc") == 0) return;
	efgrid.submitForm("ef_grid_zc", "ZM", "ZMCP15", "insert", true);
} 
button_updatedetailzc_onclick = function() {
	if (isSelected("ef_grid_zc") == 0) return; 
	efgrid.submitForm("ef_grid_zc", "ZM", "ZMCP15", "update", true);
} 
button_deletedetailzc_onclick = function() {
	var grid = isSelected("ef_grid_zc");
	if (grid == 0) return;
	EFConfirm('你确认要删除吗?', '确认框', function(r) {
		if (r) {
			efgrid.submitForm("ef_grid_zc", "ZM", "ZMCP15", "delete", true);
		}
	});
} 
/********************************************************************************
 * 钻孔从表操作
 *********************************************************************************/
button_insertdetailzk_onclick = function() {
	if (isSelected("ef_grid_zk") == 0) {
		return;
	}
	efgrid.submitForm("ef_grid_zk", "ZM", "ZMCP16", "insert", true);
} 
button_updatedetailzk_onclick = function() {
	if (isSelected("ef_grid_zk") == 0) {
		return;
	}
	efgrid.submitForm("ef_grid_zk", "ZM", "ZMCP16", "update", true);
}
button_deletedetailzk_onclick = function() {
	var grid = isSelected("ef_grid_zk");
	if (grid == 0) {
		return;
	}
	EFConfirm('你确认要删除吗?', '确认框', function(r) {
		if (r) {
			efgrid.submitForm("ef_grid_zk", "ZM", "ZMCP16", "delete", true);
		}
	});
} 

/********************************************************************************
 * 管路安装从表操作 
 *********************************************************************************/
button_insertdetailglaz_onclick = function() {
	if (isSelected("ef_grid_glaz") == 0) {
		return;
	}
	efgrid.submitForm("ef_grid_glaz", "ZM", "ZMCP17", "insert", true);
} 
button_updatedetailglaz_onclick = function() {
	if (isSelected("ef_grid_glaz") == 0) {
		return;
	}
	efgrid.submitForm("ef_grid_glaz", "ZM", "ZMCP17", "update", true);
} 
button_deletedetailglaz_onclick = function() {
	var grid = isSelected("ef_grid_glaz");
	if (grid == 0) {
		return;
	}
	EFConfirm('你确认要删除吗?', '确认框', function(r) {
		if (r) {
			efgrid.submitForm("ef_grid_glaz", "ZM", "ZMCP17", "delete", true);
		}
	});
} 
/**
 * 从表按钮显示
 */
var hideAndShowSubButtons = function(status) {

	if (!status) {
		efbutton.setButtonStatus("insertdetailzc", false);
		efbutton.setButtonStatus("updatedetailzc", false);
		efbutton.setButtonStatus("deletedetailzc", false);
		efbutton.setButtonStatus("insertdetailzk", false);
		efbutton.setButtonStatus("updatedetailzk", false);
		efbutton.setButtonStatus("deletedetailzk", false);
		efbutton.setButtonStatus("insertdetailglaz", false);
		efbutton.setButtonStatus("updatedetailglaz", false);
		efbutton.setButtonStatus("deletedetailglaz", false);
	} else {
		efbutton.setButtonStatus("insertdetailzc", true);
		efbutton.setButtonStatus("updatedetailzc", true);
		efbutton.setButtonStatus("deletedetailzc", true);
		efbutton.setButtonStatus("insertdetailzk", true);
		efbutton.setButtonStatus("updatedetailzk", true);
		efbutton.setButtonStatus("deletedetailzk", true);
		efbutton.setButtonStatus("insertdetailglaz", true);
		efbutton.setButtonStatus("updatedetailglaz", true);
		efbutton.setButtonStatus("deletedetailglaz", true);
	}
}
//新增记录行回调函数
efgrid_afterAddNewRow = function(grid_id){ 

	var grid = efgrid.getGridObject(grid_id);
	var rows = grid.getCheckedRows();	
	if(grid_id=="ef_grid_result")
	{
		var fdCreator=$('#fdCreator').val();
		var fdCreateDepartment=$('#fdCreateDepartment').val();
		var fdCreatorId=$('#fdCreatorId').val();
		var fdCreateDepartmentId=$('#fdCreateDepartmentId').val(); 
		var now = new Date();
	    var year = now.getFullYear();       //年
	    var month = now.getMonth() + 1;     //月
	    if(month > 12) {
	    	month = 1;
	    	year += 1;
	    }
	    if(month < 10) month = "0"+month;
	    var day = now.getDate();            //日 
	    var clock = year + "-" + month + "-";
	    if(day < 10) clock += "0";
	    clock += day;
	   
		for(var i = 0 ; i < rows.length;i++){
			grid.getBlockData().setCell(rows[i], "fdPlanStatus", "草拟");
			grid.getBlockData().setCell(rows[i], "fdPlanYear", year);
			grid.getBlockData().setCell(rows[i], "fdPlanMonth", month);
			grid.getBlockData().setCell(rows[i], "fdPlanType", "抽采计划");
			grid.getBlockData().setCell(rows[i], "fdTitle", year+"年"+month+"月的抽采计划");
			grid.getBlockData().setCell(rows[i], "fdCreateDate", clock);
			grid.getBlockData().setCell(rows[i], "fdYmSum", 0);
			grid.getBlockData().setCell(rows[i], "fdWscc", 0);
			grid.getBlockData().setCell(rows[i], "fdDmccl", 0);
			grid.getBlockData().setCell(rows[i], "fdWscczc", 0);
			grid.getBlockData().setCell(rows[i], "fdCcjcl", 0);
			grid.getBlockData().setCell(rows[i], "fdKjccl", 0);
			grid.getBlockData().setCell(rows[i], "fdKjccnd", 0);
		}
	}else if(grid_id=="ef_grid_zc")
	{
		for(var i = 0 ; i < rows.length;i++){
			
			grid.getBlockData().setCell(rows[i], "fdPlanValue", 0);
			grid.getBlockData().setCell(rows[i], "fdZjs", 0);
			grid.getBlockData().setCell(rows[i], "fdRs", 0);
			grid.getBlockData().setCell(rows[i], "fdPlanSx", 0);
			grid.getBlockData().setCell(rows[i], "fdPlanZx", 0);
			grid.getBlockData().setCell(rows[i], "fdPlanXx", 0);
		}
	}
	for(var i = 0 ; i < rows.length;i++){
		grid.getBlockData().setCell(rows[i], "fdKeyId", "");
	}
} 
//提交成功回调函数
efgrid_onAjaxSubmitSuccess = function(gridId, service_name, method_name, eiInfo) {
	if(gridId == "ef_grid_zk" || gridId == "ef_grid_zc" || gridId == "ef_grid_glaz") {
		if (eiInfo.status != -1) {
			var grid = efgrid.getGridObject(gridId);
			grid.refresh( eiInfo );
		}
		else
		{
			submitCallBackEiMsg = eiInfo.msg;
		}
	} else{
		if (eiInfo.status != -1) {
			var grid = efgrid.getGridObject(gridId);
			grid.refresh( eiInfo );
			$('#ef_tab_y').hide();
		}
		else
		{
			submitCallBackEiMsg = eiInfo.msg;
			flag = true;
		}
	}
};

//单元格编辑后回调的方法
efgrid_onDataCellSaved = function( grid_id, row_index, col_index, cell_value ) 
{	
	if(grid_id == "ef_grid_result"){
		var grid = efgrid.getGridObject( grid_id ); 
		if( col_index == 6 || col_index == 7) 
		{ 	
			var ymcl = grid.getCellValueByColumnName(row_index,"fdYmSum");//原煤产量
			var fdWscc = grid.getCellValueByColumnName(row_index,"fdWscc");//瓦斯抽采量
			if(ymcl!=0){
			grid.setCellValue(row_index,8,(Number(fdWscc)/Number(ymcl)).toFixed(2),TYPE_DATA);
			grid.refreshCell(row_index,8,TYPE_DATA );
			}
		}
		if( col_index == 6 || col_index == 9) 
		{ 	
			var ymcl = grid.getCellValueByColumnName(row_index,"fdYmSum");//原煤产量
			var fdWscczc = grid.getCellValueByColumnName(row_index,"fdWscczc");//吨煤瓦斯抽采量
			if(ymcl!=0){
			var value = (Number(fdWscczc)/Number(ymcl)).toFixed(2);
			grid.setCellValue(row_index,10,value,TYPE_DATA);
			grid.refreshCell(row_index,10,TYPE_DATA );
			}
		}
	}
	
	if(grid_id == "ef_grid_zc"){
		var grid = efgrid.getGridObject( grid_id );
		if( col_index == 7|| col_index == 8 || col_index == 9) 
		{
			var sx = grid.getCellValueByColumnName(row_index,"fdPlanSx");
			var zx = grid.getCellValueByColumnName(row_index,"fdPlanZx");
			var xx = grid.getCellValueByColumnName(row_index,"fdPlanXx");
			grid.setCellValue(row_index,2,Number(sx)+Number(zx)+Number(xx),TYPE_DATA);
			grid.refreshCell( row_index, 2 , TYPE_DATA ); 
		} 
	}
	if(grid_id == "ef_grid_zk"){
		var grid = efgrid.getGridObject( grid_id ); 
		if( col_index == 3 || col_index == 4) 
		{ 
			var sl = grid.getCellValueByColumnName(row_index,"fdHoleLength");//数量
			var ks = grid.getCellValueByColumnName(row_index,"fdKs");//孔深(米)
			grid.setCellValue(row_index,5,Number(sl)*Number(ks),TYPE_DATA);
			grid.refreshCell( row_index, 5 , TYPE_DATA ); 
		} 
	}
}
